package com.zhihu.activiti.controller;

import com.zhihu.activiti.SecurityUtil;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.activiti.api.task.model.Task;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.runtime.TaskRuntime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ActivitiController {

    @Autowired
    private ProcessRuntime processRuntime;

    @Autowired
    private TaskRuntime taskRuntime;

    @Autowired
    private SecurityUtil securityUtil;

    @RequestMapping("/hello")
    public void testHello(){
        //认证
        securityUtil.logInAs("erdemedeiros");
        Page<Task> tasks = taskRuntime.tasks(Pageable.of(0, 10));
        if (tasks.getTotalItems()>0) {
            tasks.getContent().forEach(item ->{
                // 拾取任务
                taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(item.getId()).build());
                // 执行任务
                taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(item.getId()).build());
            });
        }

        tasks = taskRuntime.tasks(Pageable.of(0, 10));
        if (tasks.getTotalItems()>0) {
            tasks.getContent().forEach(item ->{
                System.out.println("item = " + item);
            });
        }

    }

}
